﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Dz.WorkerService.Utils
{

    internal class DeptUtils : BaseUtils
    {
        private Dictionary<string,string> detps = new Dictionary<string, string>();
        private static readonly Lazy<DeptUtils> instance = new Lazy<DeptUtils>(() => new DeptUtils());

        public static DeptUtils Instance => instance.Value;
        protected override void Init()
        {
            string sql = @"select a.code + '/' + b.code as dept,a.DescFlexField_PrivateDescSeg2 as cust
                        from CBO_Department a
                        inner join Base_Organization b on a.org = b.ID";

            DataTable dt = sqlUtils.FillSync(sql);

            foreach (DataRow dr in dt.Rows)
            {
                detps.Add(dr[0].ToString(), dr[1].ToString());
            }
        }

        public string GetCust(string dept,string org)
        {
            string key = $"{dept}/{org}";

            if (detps.ContainsKey(key)) return detps[key];  
            
            return string.Empty;
        }

        public override void ReLoad()
        {
            detps.Clear();
            Init();
        }
    }
}
